/*
 *  StickFigure.m
 *  pogostick
 *
 *  Created by daddy (chuck@kollar.com) on 8/22/09.
 *  Copyright 2009 Charles P. Kollar All rights reserved.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "StickFigure.h"
#import "Menu.h"
#import "ChipmunkPhys.h"


// set in menu.m
extern int soundp, musicp;


@implementation StickFigure

@synthesize identifier;
@synthesize space;
@synthesize stickHeight;
@synthesize gravity;
@synthesize riderMass;
@synthesize group;
@synthesize rSholderJoint;
@synthesize lSholderJoint;
@synthesize rElbowJoint;
@synthesize lElbowJoint;
@synthesize rHipJoint;
@synthesize lHipJoint;
@synthesize rKneeJoint;
@synthesize lKneeJoint;
@synthesize staticJoint;
@synthesize shootingHand;


-(id) init
{
	if (self = [super init]) {
        shootingHand = 0; // not shooting...
	}
	return self;
}

- (void) die
{
	if (staticJoint != NULL) {
		cpSpaceRemoveConstraint(space, staticJoint);
		cpConstraintFree(staticJoint);
		staticJoint = NULL;
	}
	// remove the constraints on the other joints...
	[rSholderJoint setJointMaxForce: 0.0f];
	[lSholderJoint setJointMaxForce: 0.0f];
	[rElbowJoint setJointMaxForce: 0.0f];
	[lElbowJoint setJointMaxForce: 0.0f];
	[rHipJoint setJointMaxForce: 0.0f];
	[lHipJoint setJointMaxForce: 0.0f];
	[rKneeJoint setJointMaxForce: 0.0f];
	[lKneeJoint setJointMaxForce: 0.0f];
    
    shootingHand = 0; // not shooting...
    
    if (soundp != 0) {
        [[SimpleAudioEngine sharedEngine] playEffect: PERSON_DIES_WAV];
    }
    
    //NSLog(@"StickFigure dies!");
}


- (void) bleed
{
	// randomly creates a blood circle if the person is bleeding
	if (blood >= MAXBLOOD) return;
	if (staticJoint != NULL) {
		[self die];
	}
	blood++;
	
	cpFloat radius = (rand() % 2) + 1.0f;
	cpBody *dropletBody = cpBodyNew(1.0f, cpMomentForCircle(1.0f, 0.0f, radius, cpvzero));
	dropletBody->p = trunkBody->p;
	cpSpaceAddBody(space, dropletBody);
	cpShape *shape = cpCircleShapeNew(dropletBody, radius, cpvzero);
	shape->e = 0.0; shape->u = 2.5;
	//shape->collision_type = kColl_Blood;
	cpSpaceAddShape(space, shape);  
	float x = rand() % 40;
	float y = rand() % 40;
	x -= 20;
	y -= 20;
	cpBodyApplyImpulse(dropletBody, cpv(x, y), cpv(10.0f,20.0f));
}

- (void) shoot
{
    // determine if we do anything during this period....
	if (shootingHand == 0 || rand()%100 != 1) return;
    
    // create the bullet...
	cpFloat radius = 3.0f;
	cpBody *bulletBody = cpBodyNew(1.0f, cpMomentForCircle(1.0f, 0.0f, radius, cpvzero));
    // pick the right lower arm for if to come from...
    cpBody *armLBody = (shootingHand == 2) ? armRLBody : armLLBody;
	bulletBody->p = armLBody->p;
	cpSpaceAddBody(space, bulletBody);
	cpShape *shape = cpCircleShapeNew(bulletBody, radius, cpvzero);
	shape->e = 0.0f; shape->u = 2.5f;
	shape->collision_type = kColl_Bullet;
	cpSpaceAddShape(space, shape);
	
	// The body initial position is assumed to be 0 or 2*M_PI. Since an arm represents a stick
	// that runs up and down (M_PI/2) shifted from the assumed position, this must be factored in.
	// This sends an impluse of magnitude 800 in the direction of the angle of the body.
	cpBodyApplyImpulse(bulletBody, cpvmult(cpvforangle(armLBody->a-M_PI/2), 800.0f), cpvzero);
    if (soundp != 0) {
        [[SimpleAudioEngine sharedEngine] playEffect: GUN_FIRE_WAV];
    }
    
    //NSLog(@"StickFigure shoot! shootingHand = %u", shootingHand);
}

// Every joint in a person get's fired here....
-(void) move
{
	[rSholderJoint move];
	[lSholderJoint move];
	[rElbowJoint move];
	[lElbowJoint move];
	[rHipJoint move];
	[lHipJoint move];
	[rKneeJoint move];
	[lKneeJoint move];
}

-(void) performActions
{
    [self move];
    [self bleed];
    [self shoot];
}

-(void) dealloc {
	//cpBodyFree(trunkBody); cpBodyFree(headBody);
	//cpBodyFree(armRUBody); cpBodyFree(armRLBody); cpBodyFree(armLUBody); cpBodyFree(armLLBody);
	//cpBodyFree(legRUBody); cpBodyFree(legRLBody); cpBodyFree(legLUBody); cpBodyFree(legLLBody);
	//(neckJoint);
    
    [rSholderJoint release];
    [lSholderJoint release];
	[rElbowJoint release];
    [lElbowJoint release];
    [rHipJoint release];
    [lHipJoint release];
	[rKneeJoint release];
    [lKneeJoint release];
    
    [super dealloc];
}

@end
